<?php
class model_companies extends CI_Model {

	public function __construct()
	{
	//ini_set('memory_limit', '-1');
		$this->load->library('fclass');
		$this->load->database();
	}
	
	public function select($data)
	{		
		//echo $this->fclass->test();
		$this->db->select('*');
		$this->db->from('tbl_companies');
		$this->db->join('tbl_comp_custs', 'tbl_comp_custs.comp_id = tbl_companies.comp_id');
		$this->db->join('tbl_customers', 'tbl_customers.cust_id = tbl_comp_custs.cust_id');
		$this->db->join('tbl_cust_users', 'tbl_cust_users.cust_id = tbl_comp_custs.cust_id');

		if(isset($data["cust_creator_id"])&&$data["cust_creator_id"]!="")
			$data["array_uId"][]= $data["cust_creator_id"];

		if(isset($data["array_uId"])&&count($data["array_uId"])>1)
			$this->db->or_where_in('tbl_cust_users.uId', $data["array_uId"]); 

		if(isset($data["comp_status"])&&$data["comp_status"]!="")
			$this->db->where('comp_status',$data["comp_status"]);

		if(isset($data["comp_type"])&&$data["comp_type"]!="")
			$this->db->where('comp_type',$data["comp_type"]);
		
		if(isset($data["where"])&&$data["where"]!="")
			$this->db->where("(comp_tax_code LIKE '%".$this->fclass->cleanup_text($data["where"])."%' OR comp_name LIKE '%".$this->fclass->cleanup_text($data["where"])."%' OR  comp_phone LIKE '%".$this->fclass->cleanup_text($data["where"])."%' OR comp_address LIKE '%".$this->fclass->cleanup_text($data["where"])."%')");


	//	$this->db->group_by('tbl_companies.comp_id'); 

		if(isset($data["order_by"])&&$data["order_by"]!="")
			$this->db->order_by($data["order_by"]); 

		
		$this->db->limit($data["limit"], $data["paging"]);
		$query = $this->db->get();
		//echo $this->db->last_query();
		//print_r($query);
		return $query->result_array();		
	}

	public function selectCount($data)
	{		
		$this->db->select('*');
		$this->db->from('tbl_companies');
		$this->db->join('tbl_comp_custs', 'tbl_comp_custs.comp_id = tbl_companies.comp_id');
		$this->db->join('tbl_customers', 'tbl_customers.cust_id = tbl_comp_custs.cust_id');
			$this->db->join('tbl_cust_users', 'tbl_cust_users.cust_id = tbl_comp_custs.cust_id');

		if(isset($data["cust_creator_id"])&&$data["cust_creator_id"]!="")
			$data["array_uId"][]= $data["cust_creator_id"];

		if(isset($data["array_uId"])&&count($data["array_uId"])>1)
			$this->db->or_where_in('tbl_cust_users.uId', $data["array_uId"]); 

		if(isset($data["comp_status"])&&$data["comp_status"]!="")
			$this->db->where('comp_status',$data["comp_status"]);

		if(isset($data["comp_type"])&&$data["comp_type"]!="")
			$this->db->where('comp_type',$data["comp_type"]);
		
		if(isset($data["where"])&&$data["where"]!="")
			$this->db->where("(comp_name LIKE '%".$this->fclass->cleanup_text($data["where"])."%' OR  comp_phone LIKE '%".$this->fclass->cleanup_text($data["where"])."%' OR comp_address LIKE '%".$this->fclass->cleanup_text($data["where"])."%')");



		$query = $this->db->get();
		return $query->num_rows();		
	}

	public function selectDetail($data)
	{		
		//echo "test----".$data["comp_id"];
		$this->db->select('*');
		$this->db->from('tbl_companies');
		$this->db->join('tbl_comp_custs', 'tbl_comp_custs.comp_id = tbl_companies.comp_id');
		$this->db->join('tbl_customers', 'tbl_customers.cust_id = tbl_comp_custs.cust_id');
		$this->db->join('tbl_cust_users', 'tbl_cust_users.cust_id = tbl_comp_custs.cust_id');

		if(isset($data["cust_creator_id"])&&$data["cust_creator_id"]!="")
			$data["array_uId"][]= $data["cust_creator_id"];
		
		if(isset($data["array_uId"])){
			if(count($data["array_uId"])>1)
			$this->db->or_where_in('tbl_cust_users.uId', $data["array_uId"]); 
		}
		

		$this->db->where('tbl_companies.comp_id', $data["comp_id"]); 
		$query = $this->db->get();
		//print_r($query);
		//echo $this->db->last_query();
		return $query->result_array();		
	}

	public function selectToCustID($data)
	{
		$this->db->select('*');
		$this->db->from('tbl_companies');
		$this->db->join('tbl_comp_custs', 'tbl_comp_custs.comp_id = tbl_companies.comp_id');
		
		$this->db->where('cust_id', $data["cust_id"]); 
		$query = $this->db->get();
		return $query->result_array();
	}


	public function insert($data)
	{
		$dt = $data["arrayComp"];
		$this->db->trans_start();
		$this->db->insert('tbl_companies', $dt); 
		$insert_id = $this->db->insert_id();
		$this->db->trans_complete();
		return $insert_id;
	}

	public function checkExists($data)
	{
		$query="SELECT * FROM `tbl_companies`
		WHERE (UPPER(comp_phone) =  ?  AND comp_phone!='') 
		OR (UPPER(comp_tax_code) = ? AND comp_tax_code!='' ) ";		

		$array=array(
			strtoupper($data["comp_phone"]),
			strtoupper($data["comp_tax_code"])
			);

		

		$result	= $this->db->query($query,$array);	
		return $result->row_array();
	}

	public function update($data,$id){
		$this->db->where('comp_id', $id);
		$this->db->update('tbl_companies', $data); 
	//	echo $this->db->last_query();
		//$query = $this->db->get();
	}

	public function selectConflict($data)
	{		
		$this->db->select('*');
		$this->db->from('tbl_companies');
		$this->db->where('comp_status', $data['comp_status']); 
		$query = $this->db->get();
		return $query->result_array();		
	}

	public function delete($data){
		$this->db->delete('tbl_companies', $data); 
	}

	public function updateCompCust($data,$id){
		$this->db->where('comp_id', $id);
		$this->db->update('tbl_comp_custs', $data); 
	}

	public function selectIDDetail($data)
	{		
		$this->db->select('*');
		$this->db->from('tbl_companies');
		$this->db->where('comp_id', $data['comp_id']); 
		$query = $this->db->get();
		return $query->row_array();
	}
}

?>